home *** CD-ROM | disk | FTP | other *** search
/ PC go! 2017 October / PCgo 10-2017 CD-ROM Germany.iso / nw.pak / Unnamed File 005267.txt < prev    next >
Encoding:
Text File  |  2015-07-29  |  6.2 KB  |  173 lines

  1. // Copyright (c) 2012 Intel Corp
  2. // Copyright (c) 2014 The Chromium Authors
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. //  in the Software without restriction, including without limitation the rights
  7. //  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell co
  8. // pies of the Software, and to permit persons to whom the Software is furnished
  9. //  to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in al
  12. // l copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IM
  15. // PLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNES
  16. // S FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  17. //  OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WH
  18. // ETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. //  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20.  
  21. function DesktopCaptureMonitor() {
  22.     nw.allocateObject(this, {});
  23.     this.sources = new Array();
  24.     this.started = false;
  25. }
  26. require('util').inherits(DesktopCaptureMonitor, exports.Base);
  27.  
  28.  
  29. DesktopCaptureMonitor.prototype.start = function (screens, windows) {
  30.     if (this.started)
  31.         return false;
  32.     this.started = true;
  33.     nw.callObjectMethodSync(this, 'start', [screens, windows]);
  34.     return true;
  35. }
  36.  
  37. DesktopCaptureMonitor.prototype.stop = function () {
  38.     if (!this.started)
  39.         return false;
  40.     nw.callObjectMethodSync(this, 'stop', []);
  41.     this.started = false;
  42.     this.sources = new Array();
  43.     return true;
  44. }
  45.  
  46. DesktopCaptureMonitor.prototype.on('__nw_desktop_capture_monitor_listner_added', function (id, name, order, type, primaryindex) {
  47.     if(this.sources.indexOf(id)!=-1)
  48.     {
  49.         //TODO: Find out what this event comes twice on some platforms
  50.         return;
  51.     }
  52.     this.sources.splice(order, 0, id);
  53.     this.emit("added", id, name, order, type, primaryindex);
  54.     for (var i = order + 1; i <= this.sources.length - 1; i++) {
  55.         this.emit("orderchanged", this.sources[i], i, i - 1);
  56.     }
  57. });
  58.  
  59.  
  60. DesktopCaptureMonitor.prototype.on('__nw_desktop_capture_monitor_listner_removed', function (index) {
  61.     var id = this.sources[index];
  62.     if (index != -1) {
  63.         this.sources.splice(index, 1);
  64.         this.emit("removed", id);
  65.         for (var i = index; i <= this.sources.length - 1; i++) {
  66.             this.emit("orderchanged", this.sources[i], i, i + 1);
  67.         }
  68.     }
  69. });
  70.  
  71. DesktopCaptureMonitor.prototype.on('__nw_desktop_capture_monitor_listner_moved', function (id, new_index, old_index) {
  72.     var temp = this.sources[old_index];
  73.     this.sources.splice(old_index, 1);
  74.     this.sources.splice(new_index, 0, temp);
  75.     this.emit("orderchanged", temp, new_index, old_index);
  76.     for (var i = new_index; i < old_index; i++)
  77.         this.emit("orderchanged", this.sources[i + 1], i + 1, i);
  78. });
  79.  
  80. DesktopCaptureMonitor.prototype.on('__nw_desktop_capture_monitor_listner_namechanged', function (id, name) {
  81.     this.emit("namechanged", id, name);
  82. });
  83.  
  84. DesktopCaptureMonitor.prototype.on('__nw_desktop_capture_monitor_listner_thumbnailchanged', function (id, thumbnail) {
  85.     this.emit("thumbnailchanged", id, thumbnail);
  86. });
  87.  
  88. var listenerCount=0;
  89. DesktopCaptureMonitor.prototype.on = DesktopCaptureMonitor.prototype.addListener = function (ev, callback) {
  90.     //throw except if unsupported event
  91.     if (ev != "added" && ev != "removed" && ev != "orderchanged" && ev != "namechanged" && ev != "thumbnailchanged")
  92.         throw new String("only following events can be listened: added, removed, moved, namechanged, thumbnailchanged");
  93.  
  94.     process.EventEmitter.prototype.addListener.apply(this, arguments);
  95. }
  96.  
  97.  
  98. exports.DesktopCaptureMonitor=DesktopCaptureMonitor;
  99.  
  100. var screenInstance = null;
  101.  
  102. function Screen() {
  103.   nw.allocateObject(this, {});
  104.   this._numListener = 0;
  105.   this.DesktopCaptureMonitor = new DesktopCaptureMonitor();
  106. }
  107. require('util').inherits(Screen, exports.Base);
  108.  
  109. // Override the addListener method.
  110. Screen.prototype.on = Screen.prototype.addListener = function(ev, callback) {
  111.   if ( ev != "displayBoundsChanged" && ev != "displayAdded" && ev != "displayRemoved" && ev != "chooseDesktopMedia")
  112.     throw new TypeError('only following event can be listened: displayBoundsChanged, displayAdded, displayRemoved');
  113.   
  114.   var onRemoveListener = function (type, listener) {
  115.     if (this._numListener > 0) {
  116.       this._numListener--;
  117.       if (this._numListener == 0) {
  118.         process.EventEmitter.prototype.removeListener.apply(this, ["removeListener", onRemoveListener]);
  119.         nw.callStaticMethodSync('Screen', 'RemoveScreenChangeCallback', [ this.id ]);
  120.       }
  121.     }
  122.   };
  123.  
  124.   if(this._numListener == 0) {
  125.     if (nw.callStaticMethodSync('Screen', 'AddScreenChangeCallback', [ this.id ])[0] == false ) {
  126.       throw new Error('nw.callStaticMethodSync(Screen, AddScreenChangeCallback) fails');
  127.       return;
  128.     }
  129.     process.EventEmitter.prototype.addListener.apply(this, ["removeListener", onRemoveListener]);
  130.   }
  131.   
  132.   // Call parent.
  133.   process.EventEmitter.prototype.addListener.apply(this, arguments);
  134.   this._numListener++;
  135. }
  136.  
  137. // Route events.
  138. Screen.prototype.handleEvent = function(ev) {
  139.   if (ev != "chooseDesktopMedia")
  140.     arguments[1] = JSON.parse(arguments[1]);
  141.   // Call parent.
  142.   this.emit.apply(this, arguments);
  143. }
  144.  
  145. Screen.prototype.__defineGetter__('screens', function() {
  146.   return JSON.parse(nw.callStaticMethodSync('Screen', 'GetScreens', [ ]));
  147. });
  148.  
  149. Screen.prototype.chooseDesktopMedia = function(array, callback) {
  150.   if(nw.callStaticMethodSync('Screen', 'ChooseDesktopMedia', [ this.id, array ])[0]) {
  151.     this.once('chooseDesktopMedia', callback);
  152.     return true;
  153.   }
  154.   return false;
  155. }
  156.  
  157. Screen.prototype.cancelChooseDesktopMedia = function() {
  158.   return nw.callStaticMethodSync('Screen', 'CancelChooseDesktopMedia', [ this.id ])[0];
  159. }
  160. // ======== Screen functions End ========
  161.  
  162. // Store App object in node's context.
  163. exports.Screen = {
  164. Init: function() {
  165.   if (screenInstance == null) {
  166.     screenInstance = new Screen();
  167.   }
  168.   exports.Screen = screenInstance;
  169.   return screenInstance;
  170. }
  171. };
  172.  
  173.